<template>
	<loading-wait v-if="loading" ></loading-wait>
	<view class="uni-global-box">
		<view class="uni-global-title">
			权限管理 / 权限列表
		</view>
		<view class="">
			<view class="class-manage-top">
				<view class=""> 
					<u-button type="primary" size="mini" @click="goUrl('./add?total='+count)" >
						<text class="iconfont icon-shuliang-zengjia"></text>
						<text> &nbsp;新 增</text>
					</u-button>
				</view>
				
			</view>
			<u-table class="u-table-senior">
				<u-tr style="margin-right:15px;">
					<u-th class="u-table-th">唯一ID</u-th>
					<u-th class="u-table-th">角色名称</u-th>
					<u-th class="u-table-th">备注</u-th>
					<u-th class="u-table-th">创建时间</u-th>
					<u-th width="150px">操作</u-th>
				</u-tr>
				<view class="u-table-senior-body" style="height:65vh">
					<loaders v-if="data==0"></loaders>
					<u-tr v-for="(item,index) in data" :key="index" class="u-table-tr">
						<u-td class="u-table-td">
							{{item.role_id}}
						</u-td>
						<u-td class="u-table-td">
							{{item.role_name}}
						</u-td>
						<u-td class="u-table-td">
							{{item.comment}}
						</u-td>
						<u-td class="u-table-td">
							<uni-dateformat :date="item.create_date"></uni-dateformat>
						</u-td>
						<u-td class="u-table-td " width="150px">
							<view class="u-table-td-util">
								<u-button size="mini" @click="goUrl('./update?uid='+item._id)" type="primary" >
									<text class="iconfont icon-xiugai"></text>
									<text>修改</text>
								</u-button>
								<u-button size="mini" type="error" @click="removeData(item,index)"  :disabled="item.role_id == 'admin'" >
									<text class="iconfont icon-lajitong"></text>
									<text>删除</text>
								</u-button>
							</view>

						</u-td>
					</u-tr>
				</view>
				<view class="u-table-senior-pagination">
					<uni-pagination @change="enventPages" :total="count"></uni-pagination>
				</view>
			</u-table>
		</view>


	</view>
	<u-toast ref="uToast" />
</template>

<script>
	const global = uniCloud.importObject("global",{customUI:true});
	const Roles = uniCloud.importObject("roles",{customUI:true});
	export default {
		data() {
			return {
				loading:false,
				data: [],
				where: {},
				// 分页
				limit:10,
				skip:0,
				count:0,
			}
		},
		onLoad() {
			this.getData();
		},
		methods: {
			getData() {
				global.getList('roles',this.where,"sort asc","_id,role_name,role_id,permission,comment,create_date" ,this.skip,this.limit).then(result => {
					const data = result.data ;
					this.count = result.count;
					this.data = data;
					this.loading = false
				})
			},
			// 删除数据
			async removeData(item,index){
				const modal = await uni.showModal({title:"您确认删除吗?",content:"数据删除后不可回复,请慎重"}) ;
				if(!modal.confirm) return ;
				if(!item._id) return ;
				this.loading = true;
				const admin = await global.getList('admin',{"role":item._id },"_id asc","_id,name");
				if(admin.data.length != 0){
					this.loading = false ;
					this.$refs.uToast.show({title:'当前角色下已有管理员',type: 'error',duration:1500,position:'top'})
				}
				const result = await Roles.deleteData(item._id);
				if(result.code == 200){
					this.$refs.uToast.show({title:'删除成功',type: 'success',duration:1500,position:'top',
						callback:()=>{
							this.data.splice(index,1);
							this.loading = false;
						}
					})
				}
			},
			// 分页
			enventPages(val){
				this.loading = true
				this.skip = ( val.current-1) * 10 ;
				this.getData()
			},
			goUrl(url){
				uni.navigateTo({url})
			},
			// 图片放大
			eventImage(image) {
				if(typeof image == 'string'){
					image = [image];
				}
				uni.previewImage({
					urls: image,
				});
			},
		}
	}
</script>

<style  scoped lang="scss" >
	.class-manage-top{
		display: flex;
		padding:5px 20px ;
		justify-content:flex-end;
	}
</style>